<template>
  <div>
<!-- 菜单栏缩放按钮-->
    <div class="navbar">
      <div @click="toActive">
        <Hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container"/>
      </div>
<!--      顶部导航栏右部-->
      <div class="right-menu">
        <el-button icon="el-icon-s-home" type="text" size="medium" @click="goHome"></el-button>
        <!--  头像-->
<!--        <button @click="test">测试</button>-->
        <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
          <div class="avatar-wrapper" v-if=userInfo>
            <img :src="userInfo.userFace" class="user-avatar"/>
            <i class="el-icon-caret-bottom" />
          </div>
          <el-dropdown-menu slot="dropdown" style="margin-right: 20px">
            <router-link to="/adminInfo">
              <el-dropdown-item>个人信息</el-dropdown-item>
            </router-link>
            <router-link to="/">
              <el-dropdown-item>Dashboard</el-dropdown-item>
            </router-link>
            <a target="_blank" href="https://github.com/PanJiaChen/vue-element-admin/">
              <el-dropdown-item>Github</el-dropdown-item>
            </a>
            <a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/#/">
              <el-dropdown-item>Docs</el-dropdown-item>
            </a>
            <el-dropdown-item divided @click.native="logout">
              <span style="display:block;">Log Out</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>

</template>
<script>

import Hamburger from "../../../components/Hamburger";
import LoginApi from "@/api/login"
export default {
  name: "Navbar",
  props:['userInfo'],
  components: {
    Hamburger
  },
  data(){
    return {
      sidebar:{
        opened: false,
        isCollapse: false,
      },
        _beforeUnload_time:'',
        _gap_time:'',

      // userInfo: ''
    }
  },
  methods: {
    // 跳转主页面
    goHome(){
      if (this.$route.path === '/home'){
        return;
      }
      this.$router.push('/home')
    },
    // 方块点击导航栏收缩功能
    toActive(){
      if (this.sidebar.opened === true){
        this.sidebar.opened = false;
        this.sidebar.isCollapse = false;
        this.$emit('isCollapse',this.sidebar.isCollapse)
        return;
      }
      if (this.sidebar.opened === false){
        this.sidebar.opened = true;
        this.sidebar.isCollapse = true;
        this.$emit('isCollapse',this.sidebar.isCollapse)
        return;
      }
    },
    // 退出登录
    logout(){
      LoginApi.logout().then(res => {
        console.log(res);
        // 清空用户信息
        sessionStorage.removeItem('token');
        sessionStorage.removeItem('userInfo');
        // 清空菜單
        this.$store.commit('initRoutes',[]);
        // 跳转路由
        this.$router.replace('/')

      })
    },
    userInfo(){
      this.$router.push("/adminInfo")
    },
    beforeunloadHandler(e){
      this._beforeUnload_time = new Date().getTime();

    },
    unloadHandler(e){
      this._gap_time = new Date().getTime() - this._beforeUnload_time;
      //判断是窗口关闭还是刷新
      if (this._gap_time <= 5) {
        // 退出登录
        this.logout()
      }
    },

  },
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.addEventListener('unload', e => this.unloadHandler(e))
  },
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
    window.removeEventListener('unload', e => this.unloadHandler(e))
  }



}
</script>

<style lang="scss" scoped>
.navbar {
    height: 50px;
    overflow: hidden;
    position: relative;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
